import requests

from DAY7.项目实战DAY1.utils.GetKeywords import GetKeyword
from DAY7.项目实战DAY1.utils.SendMethod import SendMethod


class MemberInterface:
    def __init__(self, url):
        self.url = url

    # 获取响应数据
    def get_autocode(self, telephone):
        method = 'get'
        url = self.url + "/sso/getAuthCode"
        payload = {"telephone": telephone}
        return SendMethod.send_method(method, url=url, params=payload)

    # 提取验证码
    def get_verify_code(self, telephone):
        resp = self.get_autocode(telephone)
        return GetKeyword.getkeyword(resp, 'data')

    # # 进行注册
    def register(self, payload):
        method = 'post'
        url = self.url + '/sso/register'
        return SendMethod.send_method(method=method, url=url, data=payload)

    # 登录
    def login(self, username, password):
        method = "post"
        url = self.url + "/sso/login"
        body = {
            "username": username,
            "password": password
        }
        return SendMethod.send_method(method, url, data=body)

    # 获取token
    def get_token(self, username, password):
        resp = self.login(username, password)
        token = GetKeyword.getkeyword(resp, 'token')
        return {'Authorization': f'Bearer {token}'}

    # 刷新token
    def refresh_token(self, username, password):
        method = "get"
        url = self.url + "/sso/refreshToken"
        return SendMethod.send_method(method=method, url=url, headers=self.get_token(username, password))

    # 获取会员信息
    def get_infomation(self, headers):
        method = "get"
        url = self.url + "/sso/info"
        return SendMethod.send_method(method, url, headers=headers)

    # 修改会员密码
    def change_password(self, password, telephone):
        method = "post"
        url = self.url + "/sso/updatePassword"
        data = {
            "authCode": self.get_verify_code(telephone),
            "password": password,
            "telephone": telephone
        }
        return SendMethod.send_method(method, url, data=data)


if __name__ == '__main__':
    url = 'http://47.108.206.100:8085'
    mi = MemberInterface(url)
    telephone = '17735794434'
    payload = {
        'username': 'KFC4355',
        'password': '09187516',
        'telephone': telephone,
        'authCode': mi.get_verify_code(telephone)
    }
    token = mi.get_token(payload['username'], payload['password'])
    # print(mi.get_autocode(telephone))
    # print(mi.get_verify_code())
    # print(mi.register(payload))
    # print(mi.login(payload['username'], payload['password']))
    # print(mi.get_token(payload['username'], payload['password']))
    # print(mi.refresh_token(payload['username'], payload['password']))
    # print(mi.get_infomation(token))
    print(mi.change_password(123456, telephone))

